/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.service.MLTextField;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.vo.MLEvent;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.text.TextField;

	/**
	 * @author Marek Brun
	 */
	public class InputTFWithIniText extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_Submit : String = 'Submit';
		
		public var inputType : Object;		static public const INPUT_TYPE_TEXT : uint = 1;		static public const INPUT_TYPE_MAIL : uint = 2;		static public const INPUT_TYPE_NUMBER : uint = 3;		static public const INPUT_TYPE_PHONE_NUMBER : uint = 4;		static public const INPUT_TYPE_PASSWORD : uint = 5;
		
		public var mltf : MLTextField;
		public var minChars : Number = 1;
		private var iniText :String;
		private var ks:KeyboardService;

		public function InputTFWithIniText(tf:TextField, InputTFWithIniText_INPUT_TYPE:uint) {
			iniText=tf.htmlText;
			mltf=MLTextField.forInstance(tf);
			mltf.addListener(this);
			inputType=InputTFWithIniText_INPUT_TYPE;
			restrict(tf, InputTFWithIniText_INPUT_TYPE);
			ks=KeyboardService.getInstance();
		}
		
		public static function restrict(tf:TextField, InputTFWithIniText_INPUT_TYPE:uint):void {
			switch(InputTFWithIniText_INPUT_TYPE){
				case INPUT_TYPE_TEXT:
					tf.restrict=null;
				break;
				case INPUT_TYPE_PASSWORD:
					tf.restrict=null;
				break;
				case INPUT_TYPE_MAIL:
					tf.restrict='A-Za-z0-9@._\\-';
				break;
				case INPUT_TYPE_PHONE_NUMBER:
					tf.restrict='0-9\\- +';
				break;
				case INPUT_TYPE_NUMBER:
					tf.restrict='0-9 ,.';
				break;
			}
		}

		public function getGotText():Boolean {
			if(mltf.getGotFocus() && mltf.tf.text==''){
				return false;
			}else{
				return mltf.tf.htmlText!=iniText;
			}
		}
		
		public function getIsMail():Boolean {
			var text:String=mltf.tf.text;
			var okChars:String = "1234567890-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@";
			var count:int = 0;
			while(count < text.length) {
				if (okChars.indexOf(text.substr(count, 1)) == -1) {
					return false;
				}
				count++;
			}
			if((text.indexOf("@") > 0) && (text.indexOf("@") == text.lastIndexOf("@"))) {
				if(((text.lastIndexOf(".") > text.indexOf("@")) && (text.lastIndexOf(".") < (text.length - 1))) && ((text.lastIndexOf(".") - text.indexOf("@")) > 1)) {
					return true;
				}
			}
		 	return false;
		}
		
		public function setIniText():void {
			mltf.tf.htmlText=iniText;
			mltf.tf.displayAsPassword=false;
		}
		
		public function setIsRedBorder(bool:Boolean):void {
			if(bool){
				mltf.tf.border=true;				mltf.tf.borderColor=0xFF0000;
			}else{
				mltf.tf.border=false;
			}
		}
		
		public function getText():String {
			if(mltf.tf.htmlText==iniText){
				return '';
			}
			return mltf.tf.text;
		}
		
		public function setText(text:String):void {
			refreshNumberRestrict();
			mltf.tf.text=text;
		}
		
		public function isValid():Boolean {
			if(getGotText()){
				if(getText().length<minChars){ return false; }
				switch(inputType){
					case INPUT_TYPE_MAIL:
						return getIsMail();
					break;
					default: return true;
				}
			}else{
				return false;
			}
		}
		
		public function chceckIsValidAndSetRedBorder():Boolean {
			var valid:Boolean=isValid();
			setIsRedBorder(!valid);
			return valid;
		}
		
		private function refreshNumberRestrict():void {
			if(inputType==INPUT_TYPE_NUMBER){
				if(getText().indexOf('.')!=-1 || getText().indexOf(',')!=-1){
					mltf.tf.restrict='0-9';
				}else{
					mltf.tf.restrict='0-9,.';
				}
			}else if(inputType==INPUT_TYPE_PASSWORD){
				mltf.tf.displayAsPassword=true;
			}
		}
		
//****************************************************************************
// EVENTS for InputTFWithIniText
//****************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			//super.onEvent(ev);
			switch(ev.hero){
				case mltf:
					switch(ev.event){
						case mltf.event_FocusIn:
							ks.addEventListener(ks.event_Down, onKSDownWhenFocus);
							if(mltf.tf.htmlText==iniText){
								mltf.tf.text='';
							}
						break;
						case mltf.event_FocusOut:
							ks.removeEventListener(ks.event_Down, onKSDownWhenFocus);
							if(mltf.tf.text==''){
								setIniText();
							}
						break;
						case mltf.event_BeforeTextInput:
							refreshNumberRestrict();
						break;
					}
				break;
			}
		}
		
		protected function onKSDownWhenFocus(event:MLEvent):void {
			if(event.info.data.keyCode==KeyboardService.KEY_ENTER){
				broadcastEvent(event_Submit);
			}
		}
		
	}

}